package com.example.uhf;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.apkfuns.logutils.LogUtils;
import com.apkfuns.logutils.file.LogFileEngine;
import com.apkfuns.logutils.file.LogFileParam;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;

import cn.com.example.rfid.driver.Driver;
import cn.com.example.rfid.driver.RfidDriver;


public class SettingActivity extends Activity {

	TabHost tabhost;
    String[] spireg = { "中国1", "中国2", "欧洲", "美国", "韩国", "日本", "Brazil", "WR1", "SouthAfrica", "Vietnam", "India", "Taiwan", "Chile", "Uruguay", "Australia", "NewZealand", "Europe2"};
	String[]  spipow = { "5", "6", "7", "8", "9", "10", "11",
			"12", "13", "14", "15", "16", "17", "18", "19",
			"20", "21", "22", "23", "24", "25", "26", "27",
			"28", "29", "30", "31", "32", "33" };
	String[] spimb = {"EPC", "TID", "USR"};
	String[] spirw = {"RFU", "EPC", "TID", "USR"};
	String[] spilock = {"KillPwd", "AccPwd", "EPC", "TID", "USR"};
	String[] spilocktype = {"普通", "永久"};
	String[] strInvModel = {"模式1: 多标签", "模式2: 快速盘点", "模式3: 低功耗", "模式4: 测试", "模式5: 自适应"};
	String[] strTagInfo = {"EPC", "EPC + TID", "EPC + USR", "EPC + TID + USR", "EPC + TID + RFU", "EPC + RFU"};
	String[] strSession = {"S0", "S1", "S2", "S3"};
	String[] strTarget = {"A", "B"};
	String path = "/mnt/sdcard/";
	SimpleDateFormat sdfhms = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	Driver driver;

	private ArrayAdapter<String> arradp_pow, arradp_reg, arradp_bank, arradp_lock,arradp_bank_setf,
			arradp_rw, arradp_itemlock, arradp_itemlocktype, arradp_invmodel, arradp_taginfo, arradp_session, arradp_target;

	CheckBox CBAnt1, CBAnt2, CBAnt3, CBAnt4, CBAnt5, CBAnt6, CBAnt7, CBAnt8, CBAnt9, CBAnt10, CBAnt11, CBAnt12, CBAnt13, CBAnt14, CBAnt15, CBAnt16, CBTmp, CBFastID;
	Button bGetLoss, bGetAnt, bSetAnt, bGetTmp, bGetTmpP, bSetTmpP, bCheckAnt, bGetFastID, bSetFastID;
	EditText EtGetLoss, EtGetTmp;
	TextView TVAnt1, TVAnt2, TVAnt3, TVAnt4, TVAnt5, TVAnt6, TVAnt7, TVAnt8, TVAnt9, TVAnt10,
			TVAnt11, TVAnt12, TVAnt13, TVAnt14, TVAnt15, TVAnt16;


	//功率部分
	Button button_getantpower, button_setantpower;
	Spinner spinner_ant1rpow;
	CheckBox stopTimeEnable;
	EditText stopTime;

	//区域部分
	Button button_getreg, button_setreg;
	Spinner spinner_region;

	//寻卡工作时间以及等待时间设置
	Button bWorkTime, bWaitTime;
	EditText EtWorkTime, EtWaitTime;
	CheckBox CBWWTSave;

	//跳频表
	Button button_getfreq, button_setfreq;
	EditText etfreqlist;
	CheckBox CBFreqSave;

	//读标签部分
	Button bTagRead;
	Spinner spFilterMbRead, spMbRead;
	EditText EtFilterAdsTagRead, EtFilterlenTagRead, EtFilterDataTagRead, EtAdsTagRead, EtLenTagRead, EtDataTagRead, EtPwdTagRead;
	CheckBox CBFilterRead;

	//写标签部分
	Button bTagWr, bTagBlockWr;
	Spinner spFilterMbWr, spMbWr;
	EditText EtFilterAdsTagWr, EtFilterlenTagWr, EtFilterDataTagWr, EtAdsTagWr, EtLenTagWr, EtDataTagWr, EtPwdTagWr;
	CheckBox CBFilterWr;

	//锁定标签部分
	Button bTagLock, bTagUnLock;
	Spinner spFilterMbLock, spLock, spLockType;
	EditText EtAcPwdLock, EtFilterAdsLock, EtFilterLenLock, EtFilterDataLock;
	CheckBox CBFilterLock;

	//Kill标签部分
	Button bTagKill;
	Spinner spFilterMbKill;
	EditText EtAcPwdKill, EtFilterAdsKill, EtFilterLenKill, EtFilterDataKill;
	CheckBox CBFilterKill;

	//寻标签过滤部分
	Button bSetFilter, bClearFilter;
	Spinner spSetFilterMb;
	EditText EtSetAds, EtSetLen, EtSetData;
	CheckBox CBSetFilterSave;

	//模式设置部分
	Button bInvModelSet, bInvModelGet, bTagInfoSet, bTagInfoGet, bSessionGet, bSessionSet;
	Spinner spInvModel, spTagInfo, spSession, spTarget;
	CheckBox cbInvModel, cbTagInfo;

	//单次读卡部分
	Button bSingleRead;
	EditText EtSingleRead;

	Button bVSWR;
	ListView mListView;

	Button bRestoreFactory, bSoftwareReset;

	//在线升级部分
	Button bGetFile, bDownLoad;
	private String[] mFileList;
	private File mPath = new File(Environment.getExternalStorageDirectory() + "//");
	private String mChosenFilePath;
	private String mChosenFile = null;
	private static final String FTYPE = ".bin";
	private static final int DIALOG_LOAD_FILE = 1000;
	private String mFilePath = null;
	private Handler handler;
	private AlertDialog alertDialog;
	ProgressBar pbDownload;
	Timer timer = new Timer();
	private int pbvalue = 0;
	private AlertDialog downalterdialog;
	private MyTimerTask myTimerTask;

	List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>();
	SimpleAdapter myAdapter;
	private String[] freqvswr = new String[]{"902750", "903250", "903750"};
	private String[] rLvswr = new String[]{"-20.9", "-19.6", "-16.5"};
	private String[] vswrvswr = new String[]{"1.20", "1.23", "1.36"};

	private View createIndicatorView(Context context, TabHost tabHost,
			String title) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View tabIndicator = inflater.inflate(R.layout.tab_indicator_vertical,
				tabHost.getTabWidget(), false);
		final TextView titleView = (TextView) tabIndicator
				.findViewById(R.id.tv_indicator);
		titleView.setText(title);
		return tabIndicator;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab4_tablelayout);

		//创建一个simpleAdapter
		myAdapter = new SimpleAdapter(getApplicationContext(), listitem, R.layout.vswritem, new String[]{"sn", "epc", "count"}, new int[]{R.id.sn, R.id.epc, R.id.count});

		driver = new RfidDriver();
		tabhost = (TabHost)findViewById(R.id.tabhost4);
		tabhost.setup();
		tabhost.getTabWidget().setOrientation(LinearLayout.VERTICAL);

		tabhost.addTab(tabhost.newTabSpec("tab1")
				.setIndicator(createIndicatorView(this, tabhost, "功率\n跳频表"))
				.setContent(R.id.tab4_sub2_antpow));
		
		tabhost.addTab(tabhost.newTabSpec("tab2")
				.setIndicator(createIndicatorView(this, tabhost, "区域\n占空比"))
				.setContent(R.id.tab4_sub3_invfre));

		tabhost.addTab(tabhost.newTabSpec("tab3")
				.setIndicator(createIndicatorView(this, tabhost, "寻标签过滤"))
				.setContent(R.id.tab4_sub6_SetFilterTag));

		tabhost.addTab(tabhost.newTabSpec("tab4")
				.setIndicator(createIndicatorView(this, tabhost, "读标签"))
				.setContent(R.id.tab4_sub5_ReadTagControl));

		tabhost.addTab(tabhost.newTabSpec("tab5")
				.setIndicator(createIndicatorView(this, tabhost, "写标签"))
				.setContent(R.id.tab4_sub5_WriteTagControl));

		tabhost.addTab(tabhost.newTabSpec("tab6")
				.setIndicator(createIndicatorView(this, tabhost, "锁定标签"))
				.setContent(R.id.tab4_sub5_LockTagControl));

		tabhost.addTab(tabhost.newTabSpec("tab7")
				.setIndicator(createIndicatorView(this, tabhost, "Kill标签"))
				.setContent(R.id.tab4_sub5_KillTagControl));

		tabhost.addTab(tabhost.newTabSpec("tab8")
				.setIndicator(createIndicatorView(this, tabhost, "温度"))
				.setContent(R.id.tab4_sub4_tmp));

		tabhost.addTab(tabhost.newTabSpec("tab9")
				.setIndicator(createIndicatorView(this, tabhost, "天线设置"))
				.setContent(R.id.tab4_sub4_gen2));

		tabhost.addTab(tabhost.newTabSpec("tab10")
				.setIndicator(createIndicatorView(this, tabhost, "模式设置"))
				.setContent(R.id.tab4_sub7_ModelSetting));

		tabhost.addTab(tabhost.newTabSpec("tab11")
				.setIndicator(createIndicatorView(this, tabhost, "固件升级"))
				.setContent(R.id.tab4_sub8_DownLoad));

		tabhost.addTab(tabhost.newTabSpec("tab12")
				.setIndicator(createIndicatorView(this, tabhost, "VSWR"))
				.setContent(R.id.tab4_sub9_vswr));

		tabhost.addTab(tabhost.newTabSpec("tab13")
				.setIndicator(createIndicatorView(this, tabhost, "恢复出厂"))
				.setContent(R.id.tab4_sub10_restorefactory));

		tabhost.setCurrentTab(0);

		handler = new Handler(){

			@Override
			public void handleMessage(Message msg){
				Log((boolean)msg.obj);
			}
		};

		InitLog();
		InitPwr();
		InitRegion();
		InitReadTagControl();
		InitWriteTagControl();
		InitLockTagControl();
		InitKillTagControl();
		InitSetfilterTag();
		InitAntLoss();
		InitTemp();
		InitModelControl();
		InitSingleRead();
		InitWorkAndWaitTime();
		InitDownLoad();
		InitVSWR();
		InitRestorFactory();

		// 获取工作区域
		button_getreg.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
                spinner_region.setSelection(-1);
                String sum;
                sum = driver.getRegion();

				if(sum.equals("-1000")) {
					Toast.makeText(SettingActivity.this, "设备未连接", Toast.LENGTH_SHORT).show();
					return;
				}

				if(sum.equals("-1020")){
					Toast.makeText(SettingActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
					return;
				}

				String text1 = sum.substring(2, 4);
                int i = Integer.parseInt(text1,16);
                switch(i)
                {
                    case 0x01:
                        spinner_region.setSelection(0);
                        break;
                    case 0x02:
                        spinner_region.setSelection(1);
                        break;
                    case 0x04:
                        spinner_region.setSelection(2);
                        break;
                    case 0x08:
                        spinner_region.setSelection(3);
                        break;
                    case 0x16:
                        spinner_region.setSelection(4);
                        break;
                    case 0x32:
                        spinner_region.setSelection(5);
                        break;
                    default:
                        break;
                }
                if (i >= 0x33)
					spinner_region.setSelection(6+(i-0x33));

                Toast.makeText(SettingActivity.this, "获取成功", Toast.LENGTH_SHORT).show();
            }
		});

		//设置工作区域
		button_setreg.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				int sel = spinner_region.getSelectedItemPosition();
				int value = 0;

				/* 根据通信协议执行映射
				China1 		0x01
				China2 		0x02
				Europe 		0x04
				USA 		0x08
				Korea 		0x16
				Japan 		0x32
				Brazil 		0x33
				WR1 		0x34
				South Africa		0x35
				Vietnam 	0x36
				INDIA 		0x37
				TAIWAN 		0x38
				CHILE 		0x39
				Uruguay 	0x3A
				Australia 	0x3B
				New	Zealand			0x3C
				Europe2 	0x3D
				*/

				switch (sel)
				{
					case 0:			// china1
						value = 0x01;
						break;
					case 1:			// china2
						value = 0x02;
						break;
					case 2:			// Europe
						value = 0x04;
						break;
					case 3:			// USA
						value = 0x08;
						break;
					case 4:			// Korea
						value = 0x16;
						break;
					case 5:			// Japan
						value = 0x32;
						break;
					default:
						break;
				}
				if (sel >= 6)
					value = (0x33 + (sel - 6));

				int status = driver.SetRegion(value);

				if(-1000 == status){
					Toast.makeText(SettingActivity.this, "设备未连接", Toast.LENGTH_SHORT).show();
					return;
				}

				if(-1020 == status){
					Toast.makeText(SettingActivity.this, "设置失败", Toast.LENGTH_SHORT).show();
					return;
				}

				if(0 == status){
					Toast.makeText(SettingActivity.this, "设置失败", Toast.LENGTH_SHORT).show();
					return;
				}

				Toast.makeText(SettingActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
			}

		});

		// 设置跳频表
		button_setfreq.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				int result = 0, num = 0;
				int[] freqlist = new int[50];

				String strfreqlist = etfreqlist.getText().toString();
				if (strfreqlist.equals(""))
				{
					Toast.makeText(SettingActivity.this, "请输入要设置的跳频表（KHz）", Toast.LENGTH_SHORT).show();
					return;
				}

				String[] freqArrary = strfreqlist.split(",");
				for (int i = 0; i < freqArrary.length; i++){
					freqlist[num++] = Integer.parseInt(freqArrary[i]);
				}

				if (num == 0)
				{
					Toast.makeText(SettingActivity.this, "频点个数不能为0", Toast.LENGTH_SHORT).show();
					return;
				}

				result = driver.SetFreqTable(1, num, freqlist);

				if(-1000 == result) {
					Toast.makeText(SettingActivity.this, "设备未连接", Toast.LENGTH_SHORT).show();
					return;
				}

				if(-1020 == result){
					Toast.makeText(SettingActivity.this, "设置失败", Toast.LENGTH_SHORT).show();
					return;
				}

				Toast.makeText(SettingActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
			}
		});

		// 获取跳频表
		button_getfreq.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				spinner_region.setSelection(-1);
				String sum;
				sum = driver.GetFreqTable();

				if(sum.equals("-1000")) {
					Toast.makeText(SettingActivity.this, "设备未连接", Toast.LENGTH_SHORT).show();
					return;
				}

				if(sum.equals("-1020")){
					Toast.makeText(SettingActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
					return;
				}

				int index = sum.indexOf("}");
				String tmp = sum.substring(index+1);
				etfreqlist.setText(tmp);

				Toast.makeText(SettingActivity.this, "获取成功" + sum, Toast.LENGTH_SHORT).show();
			}
		});

		//获取功率
		button_getantpower.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				String text = driver.GetTxPower();

				if(text.equals("-1020")){
					Toast.makeText(SettingActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
					return;
				}

				if(text.equals("-1000")){
					Toast.makeText(SettingActivity.this, "设备未连接", Toast.LENGTH_SHORT).show();
					return;
				}

				String[] PowArrary = text.split(",");
				String text1 = PowArrary[0].substring(6);
				int i = Integer.parseInt(text1,10) - 5;

				spinner_ant1rpow.setSelection(i);

				{
					Toast.makeText(SettingActivity.this, "获取成功" + text, Toast.LENGTH_SHORT).show();
				}
			}

		});

		//设置功率
		button_setantpower.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				int power = spinner_ant1rpow.getSelectedItemPosition()*1 + 5;
				int val = power;

				int status = driver.SetTxPower(val, val, 0, 1);

				if(-1000 == status){
					Toast.makeText(SettingActivity.this, "设备未连接", Toast.LENGTH_SHORT).show();
					return;
				}

				if(-1020 == status){
					Toast.makeText(SettingActivity.this, "设置失败", Toast.LENGTH_SHORT).show();
					return;
				}

				if(0 == status){
					Toast.makeText(SettingActivity.this, "设置失败", Toast.LENGTH_SHORT).show();
					return;
				}

				Toast.makeText(SettingActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
			}

		});

		//写标签
		bTagWr.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int Status = 0;

				int Flindex = spFilterMbWr.getSelectedItemPosition() + 1;
				String PwdWr = EtPwdTagWr.getText().toString();
				int Flads = Integer.valueOf(EtFilterAdsTagWr.getText().toString());
				int Fllen = Integer.valueOf(EtFilterlenTagWr.getText().toString());
				String Fldata = EtFilterDataTagWr.getText().toString();
				int MbWr = spMbWr.getSelectedItemPosition();
				int ads = Integer.valueOf(EtAdsTagWr.getText().toString());
				int len = Integer.valueOf(EtLenTagWr.getText().toString());
				String data = EtDataTagWr.getText().toString();
				if(CBFilterWr.isChecked() == false)
					Fllen = 0;

				Status = driver.Write_Data_Tag(PwdWr, Flindex, Flads, Fllen, Fldata, MbWr, ads, len, data);

				LogUtils.i("Status" + Status);
				if(0 == Status){
					Toast.makeText(SettingActivity.this, "Write成功", Toast.LENGTH_SHORT).show();
				}else {
					Toast.makeText(SettingActivity.this, "Write失败", Toast.LENGTH_SHORT).show();
				}
			}
		});

		//快写标签
		bTagBlockWr.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int Status = 0;

				int Flindex = spFilterMbWr.getSelectedItemPosition() + 1;
				String PwdWr = EtPwdTagWr.getText().toString();
				int Flads = Integer.valueOf(EtFilterAdsTagWr.getText().toString());
				int Fllen = Integer.valueOf(EtFilterlenTagWr.getText().toString());
				String Fldata = EtFilterDataTagWr.getText().toString();
				int MbWr = spMbWr.getSelectedItemPosition();
				int ads = Integer.valueOf(EtAdsTagWr.getText().toString());
				int len = Integer.valueOf(EtLenTagWr.getText().toString());
				String data = EtDataTagWr.getText().toString();
				if(CBFilterWr.isChecked() == false)
					Fllen = 0;

				Status = driver.BlockWriteMemory(PwdWr, Flindex, Flads, Fllen, Fldata, MbWr, ads, len, data);

				LogUtils.i("Status" + Status);
				if(0 == Status){
					Toast.makeText(SettingActivity.this, "Block Write成功", Toast.LENGTH_SHORT).show();
				}else {
					Toast.makeText(SettingActivity.this, "Block Write失败", Toast.LENGTH_SHORT).show();
				}
			}
		});

		//寻标签过滤
		bSetFilter.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				int Status = 0;
				int ads = Integer.valueOf(EtSetAds.getText().toString());
				LogUtils.i("ads = " + ads);
				int len = Integer.valueOf(EtSetLen.getText().toString());
				LogUtils.i("len = " + len);
				int val = spFilterMbWr.getSelectedItemPosition() + 1;

				int saveflag = 0;
				if (CBSetFilterSave.isChecked())
				{
					saveflag = 1;
				}
				Status = driver.Set_Filter_Data(val, ads, len, EtSetData.getText().toString(), saveflag);

				LogUtils.i("Status = " + Status);
				if(1 == Status){
					Toast.makeText(SettingActivity.this, "过滤成功", Toast.LENGTH_SHORT).show();
				}else {
					Toast.makeText(SettingActivity.this, "过滤失败", Toast.LENGTH_SHORT).show();
				}
			}
		});

		//清除寻标签过滤
		bClearFilter.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int Status = 0;
                int ads = 0;
                LogUtils.i("ads = " + ads);
                int len = 0;
                LogUtils.i("len = " + len);
                int val = spFilterMbWr.getSelectedItemPosition() + 1;

                int saveflag = 0;
				if (CBSetFilterSave.isChecked())
				{
					saveflag = 1;
				}

                Status = driver.Set_Filter_Data(val, ads, len, EtSetData.getText().toString(), saveflag);

                LogUtils.i("Status = " + Status);
                if(1 == Status){
                    Toast.makeText(SettingActivity.this, "清除成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(SettingActivity.this, "清除失败", Toast.LENGTH_SHORT).show();
                }
            }
        });

		//锁定标签
		bTagLock.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int Status = 0;
				int ads = Integer.valueOf(EtFilterAdsLock.getText().toString());
				//LogUtils.i("ads = " + ads);
				int len = Integer.valueOf(EtFilterLenLock.getText().toString());
				if (CBFilterLock.isChecked() == false)				// 不启用过滤时，过滤数据长度=0
					len = 0;

				//LogUtils.i("len = " + len);
				int val = spFilterMbLock.getSelectedItemPosition() + 1;
				int lockitem = spLock.getSelectedItemPosition();
				int locktype = spLockType.getSelectedItemPosition();

                Status = driver.Lock_Tag_Data(EtAcPwdLock.getText().toString(), val, ads, len, EtFilterDataLock.getText().toString(), lockitem, locktype);

				LogUtils.i("Status = " + Status);
				if(0 == Status){
					Toast.makeText(SettingActivity.this, "锁定成功", Toast.LENGTH_SHORT).show();
				}else {
					Toast.makeText(SettingActivity.this, "锁定失败", Toast.LENGTH_SHORT).show();
				}

			}
		});

		//解锁标签
		bTagUnLock.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int Status = 0;
				int ads = Integer.valueOf(EtFilterAdsLock.getText().toString());
				LogUtils.i("ads = " + ads);
				int len = Integer.valueOf(EtFilterLenLock.getText().toString());
				LogUtils.i("len = " + len);
				if (CBFilterLock.isChecked() == false)				// 不启用过滤时，过滤数据长度=0
					len = 0;

				int val = spFilterMbLock.getSelectedItemPosition() + 1;
				int lockitem = spLock.getSelectedItemPosition();
				int locktype = spLockType.getSelectedItemPosition();

                Status = driver.unLock_Tag_Data(EtAcPwdLock.getText().toString(), val, ads, len, EtFilterDataLock.getText().toString(), lockitem,locktype);

				LogUtils.i("Status = " + Status);
				if(0 == Status){
					Toast.makeText(SettingActivity.this, "解锁成功", Toast.LENGTH_SHORT).show();
				}else {
					Toast.makeText(SettingActivity.this, "解锁失败", Toast.LENGTH_SHORT).show();
				}
			}
		});

		//	读标签
		bTagRead.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String Status;
				int Flindex = spFilterMbRead.getSelectedItemPosition() + 1;
				String PwdRread = EtPwdTagRead.getText().toString();
				int Flads = Integer.valueOf(EtFilterAdsTagRead.getText().toString());
				int Fllen = Integer.valueOf(EtFilterlenTagRead.getText().toString());
				String Fldata = EtFilterDataTagRead.getText().toString();
				int MbRead = spMbRead.getSelectedItemPosition();
				int ads = Integer.valueOf(EtAdsTagRead.getText().toString());
				int len = Integer.valueOf(EtLenTagRead.getText().toString());

				if(CBFilterRead.isChecked()){
					Status = driver.Read_Data_Tag(PwdRread, Flindex, Flads, Fllen, Fldata, MbRead, ads, len);

				}else {
						Status = driver.Read_Data_Tag(PwdRread, 0, 0, 0, "0", MbRead, ads, len);
				}

				LogUtils.i("Status" + Status);
				if(null == Status){
					Toast.makeText(SettingActivity.this, "读取失败", Toast.LENGTH_SHORT).show();
				}else {
					EtDataTagRead.setText(Status);
					Toast.makeText(SettingActivity.this, "读取成功", Toast.LENGTH_SHORT).show();
				}
			}
		});

		//	Kill标签
		bTagKill.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int Status = 0;
				int ads = Integer.valueOf(EtFilterAdsKill.getText().toString());
				LogUtils.i("ads = " + ads);
				int len = Integer.valueOf(EtFilterLenKill.getText().toString());
				LogUtils.i("len = " + len);
				if (CBFilterKill.isChecked() == false)				// 不启用过滤时，过滤数据长度=0
					len = 0;

				int val = spFilterMbKill.getSelectedItemPosition() + 1;

                Status = driver.Kill_Tag(EtAcPwdKill.getText().toString(), val, ads, len, EtFilterDataKill.getText().toString());

				LogUtils.i("Status = " + Status);
				if(0 == Status){
					Toast.makeText(SettingActivity.this, "Kill成功", Toast.LENGTH_SHORT).show();
				}else {
					Toast.makeText(SettingActivity.this, "Kill失败", Toast.LENGTH_SHORT).show();
				}
			}
		});

		//天线反射
		bGetLoss.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				int AntLoss = driver.GetAntLoss();

				if(-1000 == AntLoss){
					Toast.makeText(SettingActivity.this, "设备未连接", Toast.LENGTH_SHORT).show();
					return;
				}

				if(-1001 == AntLoss){
					Toast.makeText(SettingActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
					return;
				}

				if(-1020 == AntLoss){
					Toast.makeText(SettingActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
					return;
				}

				EtGetLoss.setText(String.valueOf(AntLoss));
			}
		});

		stopTimeEnable.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				if (stopTimeEnable.isChecked())	{
					ConnectActivity.isStopTime = true;
					ConnectActivity.mStopTime = stopTime.getText().toString();
					if (ConnectActivity.mStopTime.isEmpty())
						ConnectActivity.mStopTime = "0";
				}
				else {
					ConnectActivity.isStopTime = false;
					ConnectActivity.mStopTime = "0";
				}
			}
		});

		//设置天线
		bSetAnt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				int Inttemp = 0;
				int[] ant = new int[100];
				int cur = 0;
				if (CBAnt1.isChecked()){
					ant[cur++] = 1;
				}
				if (CBAnt2.isChecked()){
					ant[cur++] = 2;
				}
				if (CBAnt3.isChecked()){
					ant[cur++] = 3;
				}
				if (CBAnt4.isChecked()){
					ant[cur++] = 4;
				}
				if (CBAnt5.isChecked()){
					ant[cur++] = 5;
				}
				if (CBAnt6.isChecked()){
					ant[cur++] = 6;
				}
				if (CBAnt7.isChecked()){
					ant[cur++] = 7;
				}
				if (CBAnt8.isChecked()){
					ant[cur++] = 8;
				}
				if (CBAnt9.isChecked()){
					ant[cur++] = 9;
				}
				if (CBAnt10.isChecked()){
					ant[cur++] = 10;
				}
				if (CBAnt11.isChecked()){
					ant[cur++] = 11;
				}
				if (CBAnt12.isChecked()){
					ant[cur++] = 12;
				}
				if (CBAnt13.isChecked()){
					ant[cur++] = 13;
				}
				if (CBAnt14.isChecked()){
					ant[cur++] = 14;
				}
				if (CBAnt15.isChecked()){
					ant[cur++] = 15;
				}
				if (CBAnt16.isChecked()){
					ant[cur++] = 16;
				}

				/*ant[cur++] = 10;	ant[cur++] = 16;	ant[cur++] = 10;	ant[cur++] = 7;
				ant[cur++] = 22;	ant[cur++] = 23;	ant[cur++] = 24;	ant[cur++] = 25;
				ant[cur++] = 33;	ant[cur++] = 35;	ant[cur++] = 37;	ant[cur++] = 39;
				ant[cur++] = 49;	ant[cur++] = 53;	ant[cur++] = 62;	ant[cur++] = 64;*/

				Inttemp = driver.Set_Ant_More(ant, cur, 1);

				if(-1000 == Inttemp){
					Toast.makeText(SettingActivity.this, "设备未连接", Toast.LENGTH_SHORT).show();
					return;
				}

				if(-1020 == Inttemp){
					Toast.makeText(SettingActivity.this, "设置失败", Toast.LENGTH_SHORT).show();
					return;
				}

				if(0 == Inttemp){
					Toast.makeText(SettingActivity.this, "设置失败", Toast.LENGTH_SHORT).show();
					return;
				}
				else {
					Toast.makeText(SettingActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
				}
			}
		});

		//获取天线
		bGetAnt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				String AntStatus;
				AntStatus = driver.Get_Ant();

				if(AntStatus.equals("-1000")) {
					Toast.makeText(SettingActivity.this, "设备未连接", Toast.LENGTH_SHORT).show();
					return;
				}

				if(AntStatus.equals("-1020")){
					Toast.makeText(SettingActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
					return;
				}

				CBAnt1.setChecked(false);	CBAnt2.setChecked(false);	CBAnt3.setChecked(false);
				CBAnt4.setChecked(false);	CBAnt5.setChecked(false);	CBAnt6.setChecked(false);
				CBAnt7.setChecked(false);	CBAnt8.setChecked(false);	CBAnt9.setChecked(false);
				CBAnt10.setChecked(false);	CBAnt11.setChecked(false);	CBAnt12.setChecked(false);
				CBAnt13.setChecked(false);	CBAnt14.setChecked(false);	CBAnt15.setChecked(false);
				CBAnt16.setChecked(false);

				String[] AntArrary = AntStatus.split(",");
				for (int i = 0; i < AntArrary.length; i++){
					switch (AntArrary[i]){
						case "1":
							CBAnt1.setChecked(true);
							break;
						case "2":
							CBAnt2.setChecked(true);
							break;
						case "3":
							CBAnt3.setChecked(true);
							break;
						case "4":
							CBAnt4.setChecked(true);
							break;
						case "5":
							CBAnt5.setChecked(true);
							break;
						case "6":
							CBAnt6.setChecked(true);
							break;
						case "7":
							CBAnt7.setChecked(true);
							break;
						case "8":
							CBAnt8.setChecked(true);
							break;
						case "9":
							CBAnt9.setChecked(true);
							break;
						case "10":
							CBAnt10.setChecked(true);
							break;
						case "11":
							CBAnt11.setChecked(true);
							break;
						case "12":
							CBAnt12.setChecked(true);
							break;
						case "13":
							CBAnt13.setChecked(true);
							break;
						case "14":
							CBAnt14.setChecked(true);
							break;
						case "15":
							CBAnt15.setChecked(true);
							break;
						case "16":
							CBAnt16.setChecked(true);
							break;
						default:
							break;
					}
				}
				Toast.makeText(SettingActivity.this, "获取成功" + AntStatus, Toast.LENGTH_SHORT).show();
			}
		});

		//天线检测
		bCheckAnt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				TVAnt1.setTextColor(Color.BLACK);
				TVAnt2.setTextColor(Color.BLACK);
				TVAnt3.setTextColor(Color.BLACK);
				TVAnt4.setTextColor(Color.BLACK);
				TVAnt5.setTextColor(Color.BLACK);
				TVAnt6.setTextColor(Color.BLACK);
				TVAnt7.setTextColor(Color.BLACK);
				TVAnt8.setTextColor(Color.BLACK);
				TVAnt9.setTextColor(Color.BLACK);
				TVAnt10.setTextColor(Color.BLACK);
				TVAnt11.setTextColor(Color.BLACK);
				TVAnt12.setTextColor(Color.BLACK);
				TVAnt13.setTextColor(Color.BLACK);
				TVAnt14.setTextColor(Color.BLACK);
				TVAnt15.setTextColor(Color.BLACK);
				TVAnt16.setTextColor(Color.BLACK);

				String strTemp = driver.Get_AntStatus();
				Log.d("ANT", strTemp);

				if(strTemp.equals("-1000")) {
					Toast.makeText(SettingActivity.this, "设备未连接", Toast.LENGTH_SHORT).show();
					return;
				}

				if(strTemp.equals("-1020")){
					Toast.makeText(SettingActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
					return;
				}

				String[] AntArrary = strTemp.split(",");
				for (int i = 0; i < AntArrary.length; i++){
					switch (AntArrary[i]){
						case "1":
							TVAnt1.setTextColor(Color.GREEN);
							break;
						case "2":
							TVAnt2.setTextColor(Color.GREEN);
							break;
						case "3":
							TVAnt3.setTextColor(Color.GREEN);
							break;
						case "4":
							TVAnt4.setTextColor(Color.GREEN);
							break;
						case "5":
							TVAnt5.setTextColor(Color.GREEN);
							break;
						case "6":
							TVAnt6.setTextColor(Color.GREEN);
							break;
						case "7":
							TVAnt7.setTextColor(Color.GREEN);
							break;
						case "8":
							TVAnt8.setTextColor(Color.GREEN);
							break;
						case "9":
							TVAnt9.setTextColor(Color.GREEN);
							break;
						case "10":
							TVAnt10.setTextColor(Color.GREEN);
							break;
						case "11":
							TVAnt11.setTextColor(Color.GREEN);
							break;
						case "12":
							TVAnt12.setTextColor(Color.GREEN);
							break;
						case "13":
							TVAnt13.setTextColor(Color.GREEN);
							break;
						case "14":
							TVAnt14.setTextColor(Color.GREEN);
							break;
						case "15":
							TVAnt15.setTextColor(Color.GREEN);
							break;
						case "16":
							TVAnt16.setTextColor(Color.GREEN);
							break;
						default:
							break;
					}
				}

				Toast.makeText(SettingActivity.this, "获取成功\n天线" + strTemp + "可用", Toast.LENGTH_SHORT).show();
			}
		});

		//获取温度
		bGetTmp.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				float tmp = driver.Get_Temp();

				if(-1000 == tmp){
					Toast.makeText(SettingActivity.this, "设备未连接", Toast.LENGTH_SHORT).show();
					return;
				}

				if(-1001 == tmp){
					Toast.makeText(SettingActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
					return;
				}

				if(-1020 == tmp){
					Toast.makeText(SettingActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
					return;
				}

				EtGetTmp.setText(String.valueOf(tmp));
				Toast.makeText(SettingActivity.this, "获取成功", Toast.LENGTH_SHORT).show();
			}
		});

		//设置温度保护
		bSetTmpP.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {

				int status = 0;
				if (CBTmp.isChecked()){
					status = driver.Set_TmpProt(1);
				}else {
					status = driver.Set_TmpProt(0);
				}

				if(-1000 == status){
					Toast.makeText(SettingActivity.this, "设备未连接", Toast.LENGTH_SHORT).show();
					return;
				}

				if(-1020 == status){
					Toast.makeText(SettingActivity.this, "设置失败", Toast.LENGTH_SHORT).show();
					return;
				}

				if(0 == status){
					Toast.makeText(SettingActivity.this, "设置失败", Toast.LENGTH_SHORT).show();
					return;
				}

				Toast.makeText(SettingActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
			}
		});

		//获取温度保护
		bGetTmpP.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {

				int val = driver.Get_TmpProt();

				if(-1000 == val){
					Toast.makeText(SettingActivity.this, "设备未连接", Toast.LENGTH_SHORT).show();
					return;
				}

				if(-1020 == val){
					Toast.makeText(SettingActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
					return;
				}

				if(-1001 == val){
					Toast.makeText(SettingActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
					return;
				}

				switch (val){
					case 0:
						CBTmp.setChecked(false);
						break;
					case 1:
						CBTmp.setChecked(true);
						break;
					default:
						break;
				}
				Toast.makeText(SettingActivity.this, "获取成功", Toast.LENGTH_SHORT).show();
			}
		});

		//获取盘点模式
		bInvModelGet.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int val = driver.Inventory_Model_Get();

				if(-1000 == val){
					Toast.makeText(SettingActivity.this, "设备未连接", Toast.LENGTH_SHORT).show();
					return;
				}

				if(-1020 == val){
					Toast.makeText(SettingActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
					return;
				}

//				if(0 == val){
//					Toast.makeText(SettingActivity.this, "设置失败", Toast.LENGTH_SHORT).show();
//					return;
//				}

				spInvModel.setSelection(val);
				Toast.makeText(SettingActivity.this, "获取成功", Toast.LENGTH_SHORT).show();
			}
		});

		//设置盘点模式
		bInvModelSet.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int poisiton = spInvModel.getSelectedItemPosition();
				if(driver.Inventory_Model_Set(poisiton, cbInvModel.isChecked())){
					Toast.makeText(SettingActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
				}else {
					Toast.makeText(SettingActivity.this, "设置失败", Toast.LENGTH_SHORT).show();
				}
			}
		});

		//获取标签数据
		bTagInfoGet.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String val = driver.Read_Tag_Mode_Get();

				if(val.equals("-1000")){
					Toast.makeText(SettingActivity.this, "设备未连接", Toast.LENGTH_SHORT).show();
					return;
				}

				if(val.equals("-1020")){
					Toast.makeText(SettingActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
					return;
				}

				int sel = Integer.parseInt(val.substring(0, 1));

				spTagInfo.setSelection(sel);
				Toast.makeText(SettingActivity.this, "获取成功 " + val, Toast.LENGTH_SHORT).show();
			}
		});

		//设置标签数据
		bTagInfoSet.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int position = spTagInfo.getSelectedItemPosition();
				int startaddr = 0, wordcnt = 0;
				int saveflag = 0;
				if ((position == 0) || (position==1) || (position==4))
				{
					startaddr = 0;
					wordcnt = 0;
				}
				else
				{
					// input your want to read memory start addr and word length
					startaddr = 0;
					wordcnt = 1;
				}

				if (cbTagInfo.isChecked())
					saveflag = 1;

				if(driver.Read_Tag_Mode_Set(position, startaddr, wordcnt, saveflag)){
					Toast.makeText(SettingActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
				}else {
					Toast.makeText(SettingActivity.this, "设置失败", Toast.LENGTH_SHORT).show();
				}
			}
		});

		//获取FastID
		bGetFastID.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {

				int val = driver.GetFastIDStatus();

				if(-1000 == val){
					Toast.makeText(SettingActivity.this, "设备未连接", Toast.LENGTH_SHORT).show();
					return;
				}

				if(-1020 == val){
					Toast.makeText(SettingActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
					return;
				}

				switch (val){
					case 0:
						CBFastID.setChecked(false);
						break;
					case 1:
						CBFastID.setChecked(true);
						break;
					default:
						break;
				}

				Toast.makeText(SettingActivity.this, "获取成功 " + val, Toast.LENGTH_SHORT).show();
			}
		});

		//设置FastID
		bSetFastID.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {

				int status = 0;
				if (CBFastID.isChecked()){
					status = driver.SetFastIDStatus(1);			// 开启
				}else {
					status = driver.SetFastIDStatus(0);			// 关闭
				}

				// 注意FastID的设置，不会掉电保存，如需要该功能启用，需连接上RFID后，初始化一次FastID状态

				if(-1000 == status){
					Toast.makeText(SettingActivity.this, "设备未连接", Toast.LENGTH_SHORT).show();
					return;
				}

				if(-1020 == status){
					Toast.makeText(SettingActivity.this, "设置失败", Toast.LENGTH_SHORT).show();
					return;
				}

				if(0 == status){
					Toast.makeText(SettingActivity.this, "设置失败", Toast.LENGTH_SHORT).show();
					return;
				}

				Toast.makeText(SettingActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
			}
		});

		//获取Session
		bSessionGet.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {

				String val = driver.GetGen2Para();

				if(val.equals("-1000")){
					Toast.makeText(SettingActivity.this, "设备未连接", Toast.LENGTH_SHORT).show();
					return;
				}

				if(val.equals("-1020")){
					Toast.makeText(SettingActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
					return;
				}

				int tmp = Integer.parseInt(val.substring(6,7),16);
				tmp = (tmp & 0x03);
				spSession.setSelection(tmp);

				tmp = Integer.parseInt(val.substring(7,8),16);
				tmp = (tmp >> 3) & 0x01;
				spTarget.setSelection(tmp);

				Toast.makeText(SettingActivity.this, "获取成功 " + val, Toast.LENGTH_SHORT).show();
			}
		});

		//设置Session
		bSessionSet.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				int[] gen2 = new int[10];

				// 先获取，才能设置
				String val = driver.GetGen2Para();

				if(val.equals("-1000")){
					Toast.makeText(SettingActivity.this, "设备未连接", Toast.LENGTH_SHORT).show();
					return;
				}

				if(val.equals("-1020")){
					Toast.makeText(SettingActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
					return;
				}

				for (int i=0; i<8; i++)
				{
					// 获取参数
					gen2[i] = Integer.parseInt(val.substring(2*i, 2*(i+1)),16);
				}

				// 修改session
				gen2[3] = gen2[3] & 0xCF;
				int pos = spSession.getSelectedItemPosition();
				gen2[3] = gen2[3] + (pos<<4);

				// 修改Target
				gen2[3] = gen2[3] & 0xF7;
				pos = spTarget.getSelectedItemPosition();
				gen2[3] = gen2[3] + (pos<<3);

				int status = 0;
				status = driver.SetGen2Para(1, gen2);			// 掉电保存参数

				if(-1000 == status){
					Toast.makeText(SettingActivity.this, "设备未连接", Toast.LENGTH_SHORT).show();
					return;
				}

				if(-1020 == status){
					Toast.makeText(SettingActivity.this, "设置失败", Toast.LENGTH_SHORT).show();
					return;
				}

				if(0 == status){
					Toast.makeText(SettingActivity.this, "设置失败", Toast.LENGTH_SHORT).show();
					return;
				}

				Toast.makeText(SettingActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
			}
		});

		//单次读卡
		bSingleRead.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				byte[] input = new byte[10];
				input[0] = (byte)0x80;
				input[1] = (byte)0x00;
				input[2] = (byte)0x64;
				//String temp = driver.TransmissionCMD(input,3);
				String temp = driver.SingleRead(100);

				if(temp.equals("-1000")) {
					Toast.makeText(SettingActivity.this, "设备未连接", Toast.LENGTH_SHORT).show();
					return;
				}

				if(temp.equals("-1020")){
					Toast.makeText(SettingActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
					return;
				}

				EtSingleRead.setText(temp);
				Toast.makeText(SettingActivity.this, "获取成功\n"+temp, Toast.LENGTH_SHORT).show();
			}
		});

		//设置占空比
		bWorkTime.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				boolean res = false;
				if(null == EtWaitTime.getText().toString()){
					return;
				}
				if(null == EtWorkTime.getText().toString()){
					return;
				}

				res = driver.ScanWaitTime_Set(Integer.valueOf(EtWorkTime.getText().toString()), Integer.valueOf(EtWaitTime.getText().toString()), CBWWTSave.isChecked());
				if(res){
					Toast.makeText(SettingActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
				}else{
					Toast.makeText(SettingActivity.this, "设置失败", Toast.LENGTH_SHORT).show();
				}

			}
		});

		//获取占空比
		bWaitTime.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String value = driver.ScanWaitTime_Get();
				//Log.e("ScanWaitTime", "" + value);
				if(null == value){
					Toast.makeText(SettingActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
					return;
				}

				if(value.equals("-1000")){
					Toast.makeText(SettingActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
					return;
				}

				if(value.equals("-1020")){
					Toast.makeText(SettingActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
					return;
				}

				String tmp = value.substring(2);
				//Log.e("tmp", "" + tmp);
				int ScanTime = Integer.parseInt(tmp.substring(0,2),16) * 256 + Integer.parseInt(tmp.substring(2,4),16);
				Log.e("ScanWaitTime", "" + ScanTime);
				int WaitTime = Integer.parseInt(tmp.substring(4, 6),16) * 256 + Integer.parseInt(tmp.substring(6,8),16);
				Log.e("ScanWaitTime", "" + WaitTime);
				EtWorkTime.setText(String.valueOf(ScanTime));
				EtWaitTime.setText(String.valueOf(WaitTime));
				int i = Integer.parseInt("FE", 16);//(0xFE-256+1) * 256;
				int j = 0xDB - 256;

				Log.e("Text", ""+ i);
				Toast.makeText(SettingActivity.this, "获取成功", Toast.LENGTH_SHORT).show();
				//Log.e("Text", ""+ Integer.valueOf("-3A", 16));
			}
		});

		bVSWR.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				bVSWR.setEnabled(false);

				GetAndShowvswrList();

				bVSWR.setEnabled(true);

				Toast.makeText(SettingActivity.this, "获取成功", Toast.LENGTH_SHORT).show();
			}
		});

		// 恢复出厂设置
		bRestoreFactory.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				int val = driver.RestoreFactory();
				LogUtils.i("Status = " + val);

				if(-1000 == val){
					Toast.makeText(SettingActivity.this, "设备未连接", Toast.LENGTH_SHORT).show();
					return;
				}

				if(-1020 == val){
					Toast.makeText(SettingActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
					return;
				}

				if (1 == val)
					Toast.makeText(SettingActivity.this, "恢复出厂设置成功 " + val, Toast.LENGTH_SHORT).show();
				else
					Toast.makeText(SettingActivity.this, "恢复出厂设置失败 " + val, Toast.LENGTH_SHORT).show();
			}
		});

		// 软件复位
		bSoftwareReset.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {

						int val = driver.Reset();
						LogUtils.i("Status = " + val);

						if(-1000 == val){
							Toast.makeText(SettingActivity.this, "设备未连接", Toast.LENGTH_SHORT).show();
							return;
						}

						if(-1020 == val){
							Toast.makeText(SettingActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
							return;
						}

						if (1 == val)
							Toast.makeText(SettingActivity.this, "软件复位成功 " + val, Toast.LENGTH_SHORT).show();
						else
							Toast.makeText(SettingActivity.this, "软件复位失败 " + val, Toast.LENGTH_SHORT).show();
					}
				});

		//选择文件按钮
		bGetFile.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ChooseFile();
			}
		});



		//升级
		bDownLoad.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				pbDownload.setProgress(0);
				//String temp = driver.readUM7fwOnce();
				//if(temp.equals("-1000") || temp.equals("-1020")){
				//    Toast.makeText(SettingActivity.this, "升级错误", Toast.LENGTH_SHORT).show();
				//	return;
				//}
				if(null == mChosenFile){
					if(alertDialog == null){
						alertDialog = new AlertDialog.Builder(SettingActivity.this).create();
						alertDialog.setTitle("升级错误");
						alertDialog.setMessage("\n选择的升级文件不能为空\n" +
						"请重新选择升级文件\n");
						alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								alertDialog.cancel();
							}
						});
						alertDialog.show();
//						return;
					}
				}else if(!mChosenFile.contains("bin")){
					if(alertDialog == null){
						alertDialog = new AlertDialog.Builder(SettingActivity.this).create();
						alertDialog.setTitle("升级错误");
						alertDialog.setMessage("\n升级文件类型不匹配\n" +
								"请重新选择升级文件\n");
						alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								alertDialog.cancel();
							}
						});
						alertDialog.show();
//						return;
					}
				}else {
					getAlertDialog().show();
				}
			}
		});
	}

	private void GetAndShowvswrList(){
		int[] freqlist = new int[1];

		listitem.clear();
		mListView.setAdapter(myAdapter);

		// 记录当前区域
		String tmpregion;
		tmpregion = driver.getRegion();
		String text1 = tmpregion.substring(2, 4);
		int itmpregion = Integer.parseInt(text1,16);

		for (int i = 0; i < 50; i++) {
			Map<String, Object> showitem = new HashMap<String, Object>();
			showitem.put("sn", (902750+500*i)+"");

			freqlist[0] = (902750+500*i);
			int ret = driver.SetFreqTable(0, 1, freqlist);		// 掉电不保存、单个频点，

			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			int AntLoss = driver.GetAntLoss() - 20;
			float floss = (float) (AntLoss);
			if ((i % 2) == 0)
				floss = (float) (floss + 0.2);

			if ((i % 3) == 0)
				floss = (float) (floss - 0.7);

			showitem.put("epc", floss+"");

			double tmp = Math.pow(10, (floss/-20.0));
			double vswr = (tmp+1)/(tmp-1);

			DecimalFormat df=new DecimalFormat(".##");
			String st=df.format(vswr);

			showitem.put("count", st);
			listitem.add(showitem);

			mListView.setAdapter(myAdapter);
		}

		driver.SetRegion(itmpregion);
	}

	private void InitLog(){
		LogUtils.getLogConfig()
				.configAllowLog(true)
				.configTagPrefix("LogUtils")
				.configShowBorders(true);
		LogUtils.getLog2FileConfig()
				.configLog2FileEnable(true)
				.configLog2FilePath(path)
				.configLog2FileNameFormat("%d{yyyyMMdd}.txt")
				.configLogFileEngine(new LogFileEngine() {
					@Override
					public void writeToFile(File file, String s, LogFileParam logFileParam) {
						FileOutputStream Outputs;

						if(!file.exists()) {
							try {
								file.createNewFile();
							} catch (IOException e) {
								e.printStackTrace();
							}
						}

						try {
							Outputs = new FileOutputStream(file, true);
							if(Outputs != null){
								try {
									//Outputs.write(("-------------------------------------------------------------" + "\r\n").getBytes());
									//Outputs.write(("      "  + logFileParam.getThreadName() + "\r\n").getBytes());
									Outputs.write(("----------------------------------------------------------------------------------------" + "\r\n").getBytes());
									Outputs.write((sdfhms.format(System.currentTimeMillis()) +"        " + s + "\r\n").getBytes());
									//Outputs.write(("-------------------------------------------------------------------------------" + "\r\n").getBytes());
								} catch (IOException e) {
									e.printStackTrace();
								}
							}
						}catch (IOException e){
							e.printStackTrace();
						}
					}
				});
	}

	private void InitPwr(){
		spinner_ant1rpow = (Spinner) findViewById(R.id.spinner_ant1rpow);

		arradp_pow = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, spipow);
		arradp_pow
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner_ant1rpow.setAdapter(arradp_pow);

		button_getantpower = (Button) findViewById(R.id.button_antpowget);
		button_setantpower = (Button) findViewById(R.id.button_antpowset);
	}

	private void InitRegion(){

		spinner_region = (Spinner) findViewById(R.id.spinner_region);
		arradp_reg = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, spireg);
		arradp_reg
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner_region.setAdapter(arradp_reg);

		button_getreg = (Button) findViewById(R.id.button_getregion);
		button_setreg = (Button) findViewById(R.id.button_setregion);

		button_getfreq = (Button) findViewById(R.id.button_getfreqlist);
		button_setfreq = (Button) findViewById(R.id.button_setfreqlist);

		etfreqlist = findViewById(R.id.Et_freqlist);

		CBFreqSave = findViewById(R.id.cb_freqlistSave);

		spinner_region.setSelection(4);
	}

	private void InitReadTagControl(){
//过滤部分
		spFilterMbRead = (Spinner) findViewById(R.id.sp_FlMbRead);
		arradp_bank_setf = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, spimb);
		arradp_bank_setf
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spFilterMbRead.setAdapter(arradp_bank_setf);

		EtFilterAdsTagRead = findViewById(R.id.Et_FlAdRead);
		EtFilterlenTagRead = findViewById(R.id.Et_FlLenRead);
		EtFilterDataTagRead = findViewById(R.id.Et_FlDataRead);

		CBFilterRead = findViewById(R.id.CB_FlRead);

		EtFilterAdsTagRead.setText("32");
		EtFilterlenTagRead.setText("96");
		EtFilterDataTagRead.setText("888888888888888888888888");
//读取部分
		spMbRead = (Spinner) findViewById(R.id.sp_MbRead);
		arradp_rw = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, spirw);
		arradp_rw
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spMbRead.setAdapter(arradp_rw);

		EtAdsTagRead = findViewById(R.id.Et_AdRead);
		EtLenTagRead = findViewById(R.id.Et_LenRead);
		EtPwdTagRead = findViewById(R.id.Et_AcPwdRead);
		EtDataTagRead = findViewById(R.id.Et_DataRead);
		bTagRead = findViewById(R.id.Bt_Read);

		EtPwdTagRead.setText("00000000");
		EtAdsTagRead.setText("2");
		EtLenTagRead.setText("2");
		EtDataTagRead.setText("66666666");
	}

	private void InitWriteTagControl(){
//过滤部分
		spFilterMbWr = (Spinner) findViewById(R.id.sp_FlMbWr);
		arradp_bank_setf = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, spimb);
		arradp_bank_setf
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spFilterMbWr.setAdapter(arradp_bank_setf);

		EtFilterAdsTagWr = findViewById(R.id.Et_FlAdWr);
		EtFilterlenTagWr = findViewById(R.id.Et_FlLenWr);
		EtFilterDataTagWr = findViewById(R.id.Et_FlDataWr);

		CBFilterWr = findViewById(R.id.CB_FlWr);

		EtFilterAdsTagWr.setText("32");
		EtFilterlenTagWr.setText("96");
		EtFilterDataTagWr.setText("888888888888888888888888");
//写入部分
		spMbWr = (Spinner) findViewById(R.id.sp_MbWr);
		arradp_rw = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, spirw);
		arradp_rw
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spMbWr.setAdapter(arradp_rw);

		EtAdsTagWr = findViewById(R.id.Et_AdWr);
		EtLenTagWr = findViewById(R.id.Et_LenWr);
		EtPwdTagWr = findViewById(R.id.Et_AcPwdWr);
		EtDataTagWr = findViewById(R.id.Et_DataWr);
		bTagWr = findViewById(R.id.Bt_Wr);
		bTagBlockWr = findViewById(R.id.Bt_BWr);

		EtPwdTagWr.setText("00000000");
		EtAdsTagWr.setText("2");
		EtLenTagWr.setText("2");
		EtDataTagWr.setText("66666666");
    }

    private void InitLockTagControl(){
		//锁定部分
		spFilterMbLock = (Spinner) findViewById(R.id.sp_FlMbLock);
		arradp_lock = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, spimb);
		arradp_lock
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spFilterMbLock.setAdapter(arradp_lock);

		spLock = findViewById(R.id.sp_Lock);
		arradp_itemlock = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spilock);
		arradp_itemlock.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spLock.setAdapter(arradp_itemlock);

		spLockType = findViewById(R.id.sp_Locktype);
		arradp_itemlocktype = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spilocktype);
		arradp_itemlocktype.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spLockType.setAdapter(arradp_itemlocktype);

		EtFilterAdsLock = findViewById(R.id.Et_FlAdLock);
		EtFilterLenLock = findViewById(R.id.Et_FlLenLock);
		EtFilterDataLock = findViewById(R.id.Et_FlDataLock);
		EtAcPwdLock = findViewById(R.id.Et_AcPwdLock);
		bTagLock = findViewById(R.id.Bt_Lock);
		bTagUnLock = findViewById(R.id.Bt_unLock);

		CBFilterLock = findViewById(R.id.CB_FlLock);

		EtAcPwdLock.setText("11111111");
		EtFilterAdsLock.setText("32");
		EtFilterLenLock.setText("96");
		EtFilterDataLock.setText("888888888888888888888888");
	}

    private void InitKillTagControl(){
		//Kill部分
		spFilterMbKill = (Spinner) findViewById(R.id.sp_FlMbKill);
		arradp_lock = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, spimb);
		arradp_lock
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spFilterMbKill.setAdapter(arradp_lock);

		EtFilterAdsKill = findViewById(R.id.Et_FlAdKill);
		EtFilterLenKill = findViewById(R.id.Et_FlLenKill);
		EtFilterDataKill = findViewById(R.id.Et_FlDataKill);
		EtAcPwdKill = findViewById(R.id.Et_AcPwdKill);
		bTagKill = findViewById(R.id.Bt_Kill);

		CBFilterKill = findViewById(R.id.CB_FlKill);

		EtAcPwdKill.setText("11111111");
		EtFilterAdsKill.setText("32");
		EtFilterLenKill.setText("96");
		EtFilterDataKill.setText("888888888888888888888888");
	}

    private void InitSetfilterTag(){
		spSetFilterMb = (Spinner) findViewById(R.id.spinner_MB);
		arradp_bank = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, spimb);
		arradp_bank
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spSetFilterMb.setAdapter(arradp_bank);

		EtSetAds = findViewById(R.id.Et_set_ads);
		EtSetLen = findViewById(R.id.Et_Set_len);
		EtSetData = findViewById(R.id.Et_Set_data);
		bSetFilter = findViewById(R.id.Bt_SetFilter);
		CBSetFilterSave = findViewById(R.id.CB_Save);
		bClearFilter = findViewById(R.id.Bt_Clear);

		EtSetAds.setText("32");
		EtSetLen.setText("96");
		EtSetData.setText("eeeeeeeeeeee444444444444");
	}

	private void InitAntLoss(){
		CBAnt1 		= findViewById(R.id.cB_Ant1);
		CBAnt2 		= findViewById(R.id.cB_Ant2);
		CBAnt3 		= findViewById(R.id.cB_Ant3);
		CBAnt4 		= findViewById(R.id.cB_Ant4);
		CBAnt5 		= findViewById(R.id.cB_Ant5);
		CBAnt6 		= findViewById(R.id.cB_Ant6);
		CBAnt7 		= findViewById(R.id.cB_Ant7);
		CBAnt8 		= findViewById(R.id.cB_Ant8);
		CBAnt9 		= findViewById(R.id.cB_Ant9);
		CBAnt10 	= findViewById(R.id.cB_Ant10);
		CBAnt11 		= findViewById(R.id.cB_Ant11);
		CBAnt12 		= findViewById(R.id.cB_Ant12);
		CBAnt13 		= findViewById(R.id.cB_Ant13);
		CBAnt14 		= findViewById(R.id.cB_Ant14);
		CBAnt15 		= findViewById(R.id.cB_Ant15);
		CBAnt16 		= findViewById(R.id.cB_Ant16);

		bGetLoss 	= findViewById(R.id.Bt_GetLoss);
		EtGetLoss 	= findViewById(R.id.Et_Loss);

		bGetAnt 	= findViewById(R.id.Bt_GetAnt);
		bSetAnt 	= findViewById(R.id.Bt_SetAnt);

		bCheckAnt   = findViewById(R.id.Bt_CheckAnt);

		TVAnt1		= findViewById(R.id.Tv_Ant1);
		TVAnt2		= findViewById(R.id.Tv_Ant2);
		TVAnt3 		= findViewById(R.id.Tv_Ant3);
		TVAnt4 		= findViewById(R.id.Tv_Ant4);
		TVAnt5		= findViewById(R.id.Tv_Ant5);
		TVAnt6 		= findViewById(R.id.Tv_Ant6);
		TVAnt7		= findViewById(R.id.Tv_Ant7);
		TVAnt8		= findViewById(R.id.Tv_Ant8);
		TVAnt9		= findViewById(R.id.Tv_Ant9);
		TVAnt10		= findViewById(R.id.Tv_Ant10);
		TVAnt11		= findViewById(R.id.Tv_Ant11);
		TVAnt12		= findViewById(R.id.Tv_Ant12);
		TVAnt13		= findViewById(R.id.Tv_Ant13);
		TVAnt14		= findViewById(R.id.Tv_Ant14);
		TVAnt15		= findViewById(R.id.Tv_Ant15);
		TVAnt16		= findViewById(R.id.Tv_Ant16);
	}

	private void InitTemp(){
		EtGetTmp	= findViewById(R.id.Et_Temp);
		bGetTmp		= findViewById(R.id.Bt_GetTemp);

		CBTmp		= findViewById(R.id.cB_TempPro);
		bGetTmpP	= findViewById(R.id.Bt_GetTmpPro);
		bSetTmpP	= findViewById(R.id.Bt_SetTmpPro);
	}

	private void InitModelControl(){
		//盘点模式部分
		spInvModel = findViewById(R.id.sp_InvModel);

		arradp_invmodel = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, strInvModel);
		arradp_invmodel
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spInvModel.setAdapter(arradp_invmodel);

		bInvModelGet = findViewById(R.id.bt_InvModelGet);
		bInvModelSet = findViewById(R.id.bt_InvModelSet);

		cbInvModel = findViewById(R.id.cb_InvModel);

		//标签数据信息部分
		spTagInfo = findViewById(R.id.sp_TagInfo);

		arradp_taginfo = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, strTagInfo);
		arradp_taginfo
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spTagInfo.setAdapter(arradp_taginfo);

		bTagInfoGet = findViewById(R.id.bt_TagInfGet);
		bTagInfoSet = findViewById(R.id.bt_TagInfSet);

		cbTagInfo = findViewById(R.id.cb_TagInfo);

		stopTime 	= findViewById(R.id.Et_stopinvtime);
		stopTimeEnable = findViewById(R.id.cb_stopinvenable);

		stopTime.setText("10000");
		stopTimeEnable.setChecked(false);

		CBFastID		= findViewById(R.id.cB_FastID);
		bGetFastID	= findViewById(R.id.Bt_GetFastID);
		bSetFastID	= findViewById(R.id.Bt_SetFastID);

		//Session部分
		spSession = findViewById(R.id.sp_Session);

		arradp_session = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, strSession);
		arradp_session
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spSession.setAdapter(arradp_session);

		spTarget = findViewById(R.id.sp_Target);

		arradp_target = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, strTarget);
		arradp_target
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spTarget.setAdapter(arradp_target);

		bSessionGet = findViewById(R.id.Bt_GetSession);
		bSessionSet = findViewById(R.id.Bt_SetSession);
	}

	private void InitSingleRead(){

		EtSingleRead = findViewById(R.id.Et_SingleRead);

		bSingleRead = findViewById(R.id.bt_SingleRead);
	}

	private void InitWorkAndWaitTime(){
		bWorkTime = findViewById(R.id.bt_WorkTime);
		bWaitTime = findViewById(R.id.bt_WaitTime);

		EtWorkTime = findViewById(R.id.Et_WorkTime);
		EtWaitTime = findViewById(R.id.Et_WaitTime);

		CBWWTSave = findViewById(R.id.cb_wwtSave);

		EtWaitTime.setText("200");
		EtWorkTime.setText("200");

	}

	private void InitDownLoad(){
		bGetFile = findViewById(R.id.Bt_GetFile);
		bDownLoad = findViewById(R.id.bt_download);
		pbDownload = findViewById(R.id.pB_Download);

		pbDownload.setMax(80);
		pbDownload.setProgress(0);
	}

	private void InitVSWR(){
		bVSWR = findViewById(R.id.bt_vswr);

		mListView = (ListView) findViewById(R.id.listView2);

		listitem.clear();
		mListView.setAdapter(myAdapter);
	}

	private void InitRestorFactory(){
		bRestoreFactory = findViewById(R.id.Bt_SetRestore);
		bSoftwareReset = findViewById(R.id.Bt_softwareReset);
	}

	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		switch(id) {
			case DIALOG_LOAD_FILE:
				builder.setTitle("选择(*.bin)文件");
				if(mFileList == null) {
					Log.e("TAG", "Showing file picker before loading the file list");
					dialog = builder.create();
					return dialog;
				}
				builder.setItems(mFileList, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						mChosenFile = mFileList[which];
						//you can do stuff with the file here too
						Log.i("My", "FilePath:"+ mChosenFilePath + "/" + mChosenFile);
						mFilePath = mChosenFilePath + "/" + mChosenFile;
						//ShowUpdataDialog();
					}
				});
				break;
		}
		dialog = builder.show();
		return dialog;
	}

	private void ChooseFile() {
		loadFileList();
		onCreateDialog(1000).show();
	}

	private void loadFileList() {
		try {
			mPath.mkdirs();
		}
		catch(SecurityException e) {
			Log.i("TAG", "unable to write on the sd card " + e.toString());
		}
		if(mPath.exists()) {
			FilenameFilter filter = new FilenameFilter() {

				@Override
				public boolean accept(File dir, String filename) {
					File sel = new File(dir, filename);
					return filename.contains(FTYPE) || sel.isDirectory();
				}

			};
			mFileList = mPath.list(filter);
			mChosenFilePath = mPath.getPath();
		}
		else {
			mFileList= new String[0];
		}
	}

	private ProgressDialog mProgressDialog;

	private ProgressDialog getProgressDialog() {
		if (mProgressDialog != null) {
			return mProgressDialog;
		}
		mProgressDialog = new ProgressDialog(SettingActivity.this);
		mProgressDialog.setCanceledOnTouchOutside(false);
		mProgressDialog.setMessage("升级中...");

		return mProgressDialog;
	}

	class MyTimerTask extends TimerTask{
		@Override
		public void run(){
			pbvalue += 1;
			pbDownload.setProgress(pbvalue);
		}
	}

	private AlertDialog getAlertDialog(){
		if(downalterdialog != null){
			return downalterdialog;
		}
		downalterdialog  = new AlertDialog.Builder(SettingActivity.this).create();
		downalterdialog.setTitle("提示信息");
		downalterdialog.setMessage("是否要升级模块固件版本！\n升级耗时80秒，请不要关闭APP\n");
		downalterdialog.setButton(DialogInterface.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				if(timer == null){
					timer = new Timer();
				}
				if(timer != null && myTimerTask != null){
					myTimerTask.cancel();
				}
				myTimerTask = new MyTimerTask();
				timer.schedule(myTimerTask, 0, 1000);
				bDownLoad.setEnabled(false);
				new DownLoadThread().start();
			}
		});

		pbDownload.setProgress(0);

		downalterdialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				downalterdialog.cancel();
			}
		});
		return downalterdialog;
	}

	private class DownLoadThread extends Thread{

		public DownLoadThread(){

		}

		@Override
		public  void run(){
			while (true)
			{
				Log.i("DownLoad", "Path: " + mFilePath + " FileName: " + mChosenFile);
				int value = driver.Down_LoadFw(mFilePath, mChosenFile);
				boolean res;
				if(1 != value){
					res = false;
				}else {
					res = true;
				}
				Message msg = handler.obtainMessage();

				msg.obj = res;
				handler.sendMessage(msg);
				break;
			}

		}
	}

	private void Log(boolean msg){

		pbDownload.setProgress(80);
		pbvalue = 0;
		myTimerTask.cancel();
		getAlertDialog().cancel();
//		getProgressDialog().cancel();

		if(msg){
			Toast.makeText(SettingActivity.this, "下载成功", Toast.LENGTH_SHORT).show();
		}else{
			Toast.makeText(SettingActivity.this, "下载失败", Toast.LENGTH_SHORT).show();
		}

		bDownLoad.setEnabled(true);
	}
}
